<div style="color:Black;background-color:White;"><pre>
<span style="color:Green;">// This sample will guide you through elements of the F# language.  </span>
<span style="color:Green;">//</span>
<span style="color:Green;">// *******************************************************************************************************</span>
<span style="color:Green;">//   To execute the code in F# Interactive, highlight a section of code and press Alt-Enter or right-click </span>
<span style="color:Green;">//   and select &quot;Execute in Interactive&quot;.  You can open the F# Interactive Window from the &quot;View&quot; menu. </span>
<span style="color:Green;">// *******************************************************************************************************</span>
<span style="color:Green;">//</span>
<span style="color:Green;">// For more about F#, see:</span>
<span style="color:Green;">//     http://fsharp.net</span>
<span style="color:Green;">//</span>
<span style="color:Green;">// For additional templates to use with F#, see the 'Online Templates' in Visual Studio, </span>
<span style="color:Green;">//     'New Project' --&gt; 'Online Templates'</span>
<span style="color:Green;">//</span>
<span style="color:Green;">// For specific F# topics, see:</span>
<span style="color:Green;">//     http://go.microsoft.com/fwlink/?LinkID=234174 (F# Development Portal)</span>
<span style="color:Green;">//     http://go.microsoft.com/fwlink/?LinkID=124614 (Code Gallery)</span>
<span style="color:Green;">//     http://go.microsoft.com/fwlink/?LinkId=235173 (Math/Stats Programming)</span>
<span style="color:Green;">//     http://go.microsoft.com/fwlink/?LinkId=235176 (Charting)</span>

<span style="color:Green;">// Contents:</span>
<span style="color:Green;">//    - Integers and basic functions</span>
<span style="color:Green;">//    - Booleans</span>
<span style="color:Green;">//    - Strings</span>
<span style="color:Green;">//    - Tuples</span>
<span style="color:Green;">//    - Lists and list processing</span>
<span style="color:Green;">//    - Classes</span>
<span style="color:Green;">//    - Generic classes</span>
<span style="color:Green;">//    - Implementing interfaces</span>
<span style="color:Green;">//    - Arrays</span>
<span style="color:Green;">//    - Sequences</span>
<span style="color:Green;">//    - Recursive functions</span>
<span style="color:Green;">//    - Record types</span>
<span style="color:Green;">//    - Union types</span>
<span style="color:Green;">//    - Option types            </span>
<span style="color:Green;">//    - Pattern matching        </span>
<span style="color:Green;">//    - Units of measure        </span>
<span style="color:Green;">//    - Parallel array programming</span>
<span style="color:Green;">//    - Using events</span>
<span style="color:Green;">//    - Database access using type providers</span>
<span style="color:Green;">//    - OData access using type providers</span>



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Integers and basic functions</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> Integers = 
    <span style="color:Blue;">let</span> sampleInteger = 176

    <span style="color:Gray;">///</span><span style="color:Green;"> Do some arithmetic starting with the first integer</span>
    <span style="color:Blue;">let</span> sampleInteger2 = (sampleInteger/4 + 5 - 7) * 4

    <span style="color:Gray;">///</span><span style="color:Green;"> A list of the numbers from 0 to 99</span>
    <span style="color:Blue;">let</span> sampleNumbers = [ 0 .. 99 ]

    <span style="color:Gray;">///</span><span style="color:Green;"> A list of all tuples containing all the numbers from 0 to 99 and their squares</span>
    <span style="color:Blue;">let</span> sampleTableOfSquares = [ <span style="color:Blue;">for</span> i <span style="color:Blue;">in</span> 0 .. 99 <span style="color:Blue;">-&gt;</span> (i, i*i) ]

    <span style="color:Green;">// The next line prints a list that includes tuples, using %A for generic printing</span>
    printfn <span style="color:#A31515;">&quot;The table of squares from 0 to 99 is:\n%A&quot;</span> sampleTableOfSquares


<span style="color:Blue;">module</span> BasicFunctions = 

    <span style="color:Green;">// Use 'let' to define a function that accepts an integer argument and returns an integer. </span>
    <span style="color:Blue;">let</span> func1 x = x*x + 3             

    <span style="color:Green;">// Parenthesis are optional for function arguments</span>
    <span style="color:Blue;">let</span> func1a (x) = x*x + 3             

    <span style="color:Gray;">///</span><span style="color:Green;"> Apply the function, naming the function return result using 'let'. </span>
    <span style="color:Gray;">///</span><span style="color:Green;"> The variable type is inferred from the function return type.</span>
    <span style="color:Blue;">let</span> result1 = func1 4573
    printfn <span style="color:#A31515;">&quot;The result of squaring the integer 4573 and adding 3 is %d&quot;</span> result1

    <span style="color:Green;">// When needed, annotate the type of a parameter name using '(argument:type)'</span>
    <span style="color:Blue;">let</span> func2 (x:int) = 2*x*x - x/5 + 3

    <span style="color:Blue;">let</span> result2 = func2 (7 + 4)
    printfn <span style="color:#A31515;">&quot;The result of applying the 1st sample function to (7 + 4) is %d&quot;</span> result2

    <span style="color:Blue;">let</span> func3 x = 
        <span style="color:Blue;">if</span> x &lt; 100.0 <span style="color:Blue;">then</span> 
            2.0*x*x - x/5.0 + 3.0
        <span style="color:Blue;">else</span> 
            2.0*x*x + x/5.0 - 37.0

    <span style="color:Blue;">let</span> result3 = func3 (6.5 + 4.5)
    printfn <span style="color:#A31515;">&quot;The result of applying the 2nd sample function to (6.5 + 4.5) is %f&quot;</span> result3



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Booleans</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> SomeBooleanValues = 

    <span style="color:Blue;">let</span> boolean1 = <span style="color:Blue;">true</span>
    <span style="color:Blue;">let</span> boolean2 = <span style="color:Blue;">false</span>

    <span style="color:Blue;">let</span> boolean3 = not boolean1 &amp;&amp; (boolean2 || <span style="color:Blue;">false</span>)

    printfn <span style="color:#A31515;">&quot;The expression 'not boolean1 &amp;&amp; (boolean2 || false)' is %A&quot;</span> boolean3



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Strings</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> StringManipulation = 

    <span style="color:Blue;">let</span> string1 = <span style="color:#A31515;">&quot;Hello&quot;</span>
    <span style="color:Blue;">let</span> string2  = <span style="color:#A31515;">&quot;world&quot;</span>

    <span style="color:Gray;">///</span><span style="color:Green;"> Use @ to create a verbatim string literal</span>
    <span style="color:Blue;">let</span> string3 = <span style="color:#A31515;">@&quot;c:\Program Files\&quot;</span>

    <span style="color:Gray;">///</span><span style="color:Green;"> Using a triple-quote string literal</span>
    <span style="color:Blue;">let</span> string4 = <span style="color:#A31515;">&quot;&quot;&quot;He said &quot;hello world&quot; after you did&quot;&quot;&quot;</span>

    <span style="color:Blue;">let</span> helloWorld = string1 + <span style="color:#A31515;">&quot; &quot;</span> + string2 <span style="color:Green;">// concatenate the two strings with a space in between</span>
    printfn <span style="color:#A31515;">&quot;%s&quot;</span> helloWorld

    <span style="color:Gray;">///</span><span style="color:Green;"> A string formed by taking the first 7 characters of one of the result strings</span>
    <span style="color:Blue;">let</span> substring = helloWorld.[0..6]
    printfn <span style="color:#A31515;">&quot;%s&quot;</span> substring



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Tuples (ordered sets of values)</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> Tuples = 

    <span style="color:Gray;">///</span><span style="color:Green;"> A simple tuple of integers</span>
    <span style="color:Blue;">let</span> tuple1 = (1, 2, 3)

    <span style="color:Gray;">///</span><span style="color:Green;"> A function that swaps the order of two values in a tuple. </span>
    <span style="color:Gray;">///</span><span style="color:Green;"> QuickInfo shows that the function is inferred to have a generic type.</span>
    <span style="color:Blue;">let</span> swapElems (a, b) = (b, a)

    printfn <span style="color:#A31515;">&quot;The result of swapping (1, 2) is %A&quot;</span> (swapElems (1,2))

    <span style="color:Gray;">///</span><span style="color:Green;"> A tuple consisting of an integer, a string, and a double-precision floating point number</span>
    <span style="color:Blue;">let</span> tuple2 = (1, <span style="color:#A31515;">&quot;fred&quot;</span>, 3.1415)

    printfn <span style="color:#A31515;">&quot;tuple1: %A    tuple2: %A&quot;</span> tuple1 tuple2
    


<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Lists and list processing</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> Lists = 

    <span style="color:Blue;">let</span> list1 = [ ]            <span style="color:Gray;">///</span><span style="color:Green;"> an empty list</span>

    <span style="color:Blue;">let</span> list2 = [ 1; 2; 3 ]    <span style="color:Gray;">///</span><span style="color:Green;"> list of 3 elements</span>

    <span style="color:Blue;">let</span> list3 = 42 :: list2    <span style="color:Gray;">///</span><span style="color:Green;"> a new list with '42' added to the beginning</span>

    <span style="color:Blue;">let</span> numberList = [ 1 .. 1000 ]  <span style="color:Gray;">///</span><span style="color:Green;"> list of integers from 1 to 1000</span>

    <span style="color:Gray;">///</span><span style="color:Green;"> A list containing all the days of the year</span>
    <span style="color:Blue;">let</span> daysList = 
        [ <span style="color:Blue;">for</span> month <span style="color:Blue;">in</span> 1 .. 12 <span style="color:Blue;">do</span>
              <span style="color:Blue;">for</span> day <span style="color:Blue;">in</span> 1 .. System.DateTime.DaysInMonth(2012, month) <span style="color:Blue;">do</span> 
                  <span style="color:Blue;">yield</span> System.DateTime(2012, month, day) ]

    <span style="color:Gray;">///</span><span style="color:Green;"> A list containing the tuples which are the coordinates of the black squares on a chess board.</span>
    <span style="color:Blue;">let</span> blackSquares = 
        [ <span style="color:Blue;">for</span> i <span style="color:Blue;">in</span> 0 .. 7 <span style="color:Blue;">do</span>
              <span style="color:Blue;">for</span> j <span style="color:Blue;">in</span> 0 .. 7 <span style="color:Blue;">do</span> 
                  <span style="color:Blue;">if</span> (i+j) % 2 = 1 <span style="color:Blue;">then</span> 
                      <span style="color:Blue;">yield</span> (i, j) ]

    <span style="color:Gray;">///</span><span style="color:Green;"> Square the numbers in numberList, using the pipeline operator to pass an argument to List.map    </span>
    <span style="color:Blue;">let</span> squares = 
        numberList 
        |&gt; List.map (<span style="color:Blue;">fun</span> x <span style="color:Blue;">-&gt;</span> x*x) 

    <span style="color:Gray;">///</span><span style="color:Green;"> Computes the sum of the squares of the numbers divisible by 3.</span>
    <span style="color:Blue;">let</span> sumOfSquaresUpTo n = 
        numberList
        |&gt; List.filter (<span style="color:Blue;">fun</span> x <span style="color:Blue;">-&gt;</span> x % 3 = 0)
        |&gt; List.sumBy (<span style="color:Blue;">fun</span> x <span style="color:Blue;">-&gt;</span> x * x)



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Classes</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> DefiningClasses = 

    <span style="color:Gray;">///</span><span style="color:Green;"> The class's constructor takes two arguments: dx and dy, both of type 'float'. </span>
    <span style="color:Blue;">type</span> Vector2D(dx : float, dy : float) = 
        <span style="color:Gray;">///</span><span style="color:Green;"> The length of the vector, computed when the object is constructed</span>
        <span style="color:Blue;">let</span> length = sqrt (dx*dx + dy*dy)

        <span style="color:Green;">// 'this' specifies a name for the object's self identifier.</span>
        <span style="color:Green;">// In instance methods, it must appear before the member name.</span>
        <span style="color:Blue;">member</span> this.DX = dx  

        <span style="color:Blue;">member</span> this.DY = dy

        <span style="color:Blue;">member</span> this.Length = length

        <span style="color:Blue;">member</span> this.Scale(k) = Vector2D(k * this.DX, k * this.DY)
    
    <span style="color:Gray;">///</span><span style="color:Green;"> An instance of the Vector2D class</span>
    <span style="color:Blue;">let</span> vector1 = Vector2D(3.0, 4.0)

    <span style="color:Gray;">///</span><span style="color:Green;"> Get a new scaled vector object, without modifying the original object </span>
    <span style="color:Blue;">let</span> vector2 = vector1.Scale(10.0)

    printfn <span style="color:#A31515;">&quot;Length of vector1: %f      Length of vector2: %f&quot;</span> vector1.Length vector2.Length



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Generic classes</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> DefiningGenericClasses = 

    <span style="color:Blue;">type</span> StateTracker&lt;<span style="color:#A31515;">'T&gt;(initialElement: '</span>T) = <span style="color:Green;">// 'T is the type parameter for the class</span>

        <span style="color:Gray;">///</span><span style="color:Green;"> Store the states in an array</span>
        <span style="color:Blue;">let</span> <span style="color:Blue;">mutable</span> states = [ initialElement ]

        <span style="color:Gray;">///</span><span style="color:Green;"> Add a new element to the list of states</span>
        <span style="color:Blue;">member</span> this.UpdateState newState = 
            states &lt;- newState :: states  <span style="color:Green;">// use the '&lt;-' operator to mutate the value</span>

        <span style="color:Gray;">///</span><span style="color:Green;"> Get the entire list of historical states</span>
        <span style="color:Blue;">member</span> this.History = states

        <span style="color:Gray;">///</span><span style="color:Green;"> Get the latest state</span>
        <span style="color:Blue;">member</span> this.Current = states.Head

    <span style="color:Gray;">///</span><span style="color:Green;"> An 'int' instance of the state tracker class. Note that the type parameter is inferred.</span>
    <span style="color:Blue;">let</span> tracker = StateTracker 10

    <span style="color:Green;">// Add a state</span>
    tracker.UpdateState 17



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Implementing interfaces</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Gray;">///</span><span style="color:Green;"> Type that implements IDisposable</span>
<span style="color:Blue;">type</span> ReadFile() =

    <span style="color:Blue;">let</span> file = <span style="color:Blue;">new</span> System.IO.StreamReader(<span style="color:#A31515;">&quot;readme.txt&quot;</span>)

    <span style="color:Blue;">member</span> this.ReadLine() = file.ReadLine()

    <span style="color:Green;">// this class's implementation of IDisposable members</span>
    <span style="color:Blue;">interface</span> System.IDisposable <span style="color:Blue;">with</span>    
        <span style="color:Blue;">member</span> this.Dispose() = file.Close()



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Arrays</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> Arrays = 

    <span style="color:Gray;">///</span><span style="color:Green;"> The empty array</span>
    <span style="color:Blue;">let</span> array1 = [| |]

    <span style="color:Blue;">let</span> array2 = [| <span style="color:#A31515;">&quot;hello&quot;</span>; <span style="color:#A31515;">&quot;world&quot;</span>; <span style="color:#A31515;">&quot;and&quot;</span>; <span style="color:#A31515;">&quot;hello&quot;</span>; <span style="color:#A31515;">&quot;world&quot;</span>; <span style="color:#A31515;">&quot;again&quot;</span> |]

    <span style="color:Blue;">let</span> array3 = [| 1 .. 1000 |]

    <span style="color:Gray;">///</span><span style="color:Green;"> An array containing only the words &quot;hello&quot; and &quot;world&quot;</span>
    <span style="color:Blue;">let</span> array4 = [| <span style="color:Blue;">for</span> word <span style="color:Blue;">in</span> array2 <span style="color:Blue;">do</span>
                        <span style="color:Blue;">if</span> word.Contains(<span style="color:#A31515;">&quot;l&quot;</span>) <span style="color:Blue;">then</span> 
                            <span style="color:Blue;">yield</span> word |]

    <span style="color:Gray;">///</span><span style="color:Green;"> An array initialized by index and containing the even numbers from 0 to 2000</span>
    <span style="color:Blue;">let</span> evenNumbers = Array.init 1001 (<span style="color:Blue;">fun</span> n <span style="color:Blue;">-&gt;</span> n * 2) 

    <span style="color:Gray;">///</span><span style="color:Green;"> sub-array extracted using slicing notation</span>
    <span style="color:Blue;">let</span> evenNumbersSlice = evenNumbers.[0..500]

    <span style="color:Blue;">for</span> word <span style="color:Blue;">in</span> array4 <span style="color:Blue;">do</span> 
        printfn <span style="color:#A31515;">&quot;word: %s&quot;</span> word

    <span style="color:Green;">// modify an array element using the left arrow assignment operator</span>
    array2.[1] &lt;- <span style="color:#A31515;">&quot;WORLD!&quot;</span>

    <span style="color:Gray;">///</span><span style="color:Green;"> Calculates the sum of the lengths of the words that start with 'h'</span>
    <span style="color:Blue;">let</span> sumOfLengthsOfWords = 
        array2
        |&gt; Array.filter (<span style="color:Blue;">fun</span> x <span style="color:Blue;">-&gt;</span> x.StartsWith <span style="color:#A31515;">&quot;h&quot;</span>)
        |&gt; Array.sumBy (<span style="color:Blue;">fun</span> x <span style="color:Blue;">-&gt;</span> x.Length)



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Sequences</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> Sequences = 
    <span style="color:Green;">// Sequences are evaluated on-demand and are re-evaluated each time they are iterated. </span>
    <span style="color:Green;">// An F# sequence is an instance of a System.Collections.Generic.IEnumerable&lt;'T&gt;,</span>
    <span style="color:Green;">// so Seq functions can be applied to Lists and Arrays as well.</span>

    <span style="color:Gray;">///</span><span style="color:Green;"> The empty sequence</span>
    <span style="color:Blue;">let</span> seq1 = Seq.empty

    <span style="color:Blue;">let</span> seq2 = seq { <span style="color:Blue;">yield</span> <span style="color:#A31515;">&quot;hello&quot;</span>; <span style="color:Blue;">yield</span> <span style="color:#A31515;">&quot;world&quot;</span>; <span style="color:Blue;">yield</span> <span style="color:#A31515;">&quot;and&quot;</span>; <span style="color:Blue;">yield</span> <span style="color:#A31515;">&quot;hello&quot;</span>; <span style="color:Blue;">yield</span> <span style="color:#A31515;">&quot;world&quot;</span>; <span style="color:Blue;">yield</span> <span style="color:#A31515;">&quot;again&quot;</span> }

    <span style="color:Blue;">let</span> numbersSeq = seq { 1 .. 1000 }

    <span style="color:Gray;">///</span><span style="color:Green;"> another array containing only the words &quot;hello&quot; and &quot;world&quot;</span>
    <span style="color:Blue;">let</span> seq3 = 
        seq { <span style="color:Blue;">for</span> word <span style="color:Blue;">in</span> seq2 <span style="color:Blue;">do</span>
                  <span style="color:Blue;">if</span> word.Contains(<span style="color:#A31515;">&quot;l&quot;</span>) <span style="color:Blue;">then</span> 
                      <span style="color:Blue;">yield</span> word }

    <span style="color:Blue;">let</span> evenNumbers = Seq.init 1001 (<span style="color:Blue;">fun</span> n <span style="color:Blue;">-&gt;</span> n * 2) 

    <span style="color:Blue;">let</span> rnd = System.Random()

    <span style="color:Gray;">///</span><span style="color:Green;"> An infinite sequence which is a random walk</span>
    <span style="color:Green;">//  Use yield! to return each element of a subsequence, similar to IEnumerable.SelectMany.</span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> randomWalk x =
        seq { <span style="color:Blue;">yield</span> x
              <span style="color:Blue;">yield!</span> randomWalk (x + rnd.NextDouble() - 0.5) }

    <span style="color:Blue;">let</span> first100ValuesOfRandomWalk = 
        randomWalk 5.0 
        |&gt; Seq.truncate 100
        |&gt; Seq.toList



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Recursive functions</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> RecursiveFunctions  = 
              
    <span style="color:Gray;">///</span><span style="color:Green;"> Compute the factorial of an integer. Use 'let rec' to define a recursive function</span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> factorial n = 
        <span style="color:Blue;">if</span> n = 0 <span style="color:Blue;">then</span> 1 <span style="color:Blue;">else</span> n * factorial (n-1)

    <span style="color:Gray;">///</span><span style="color:Green;"> Computes the greatest common factor of two integers. </span>
    <span style="color:Green;">//  Since all of the recursive calls are tail calls, the compiler will turn the function into a loop,</span>
    <span style="color:Green;">//  which improves performance and reduces memory consumption.</span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> greatestCommonFactor a b =                       
        <span style="color:Blue;">if</span> a = 0 <span style="color:Blue;">then</span> b
        <span style="color:Blue;">elif</span> a &lt; b <span style="color:Blue;">then</span> greatestCommonFactor a (b - a)           
        <span style="color:Blue;">else</span> greatestCommonFactor (a - b) b

    <span style="color:Gray;">///</span><span style="color:Green;"> Computes the sum of a list of integers using recursion.</span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> sumList xs =
        <span style="color:Blue;">match</span> xs <span style="color:Blue;">with</span>
        | []    <span style="color:Blue;">-&gt;</span> 0
        | y::ys <span style="color:Blue;">-&gt;</span> y + sumList ys

    <span style="color:Gray;">///</span><span style="color:Green;"> Make the function tail recursive, using a helper function with a result accumulator</span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> <span style="color:Blue;">private</span> sumListTailRecHelper accumulator xs =
        <span style="color:Blue;">match</span> xs <span style="color:Blue;">with</span>
        | []    <span style="color:Blue;">-&gt;</span> accumulator
        | y::ys <span style="color:Blue;">-&gt;</span> sumListTailRecHelper (accumulator+y) ys

    <span style="color:Blue;">let</span> sumListTailRecursive xs = sumListTailRecHelper 0 xs



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Record types</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> RecordTypes = 

    <span style="color:Green;">// define a record type</span>
    <span style="color:Blue;">type</span> ContactCard = 
        { Name     : string;
          Phone    : string;
          Verified : bool }
              
    <span style="color:Blue;">let</span> contact1 = { Name = <span style="color:#A31515;">&quot;Alf&quot;</span> ; Phone = <span style="color:#A31515;">&quot;(206) 555-0157&quot;</span> ; Verified = <span style="color:Blue;">false</span> }

    <span style="color:Green;">// create a new record that is a copy of contact1, </span>
    <span style="color:Green;">// but has different values for the 'Phone' and 'Verified' fields</span>
    <span style="color:Blue;">let</span> contact2 = { contact1 <span style="color:Blue;">with</span> Phone = <span style="color:#A31515;">&quot;(206) 555-0112&quot;</span>; Verified = <span style="color:Blue;">true</span> }

    <span style="color:Gray;">///</span><span style="color:Green;"> Converts a 'ContactCard' object to a string</span>
    <span style="color:Blue;">let</span> showCard c = 
        c.Name + <span style="color:#A31515;">&quot; Phone: &quot;</span> + c.Phone + (<span style="color:Blue;">if</span> not c.Verified <span style="color:Blue;">then</span> <span style="color:#A31515;">&quot; (unverified)&quot;</span> <span style="color:Blue;">else</span> <span style="color:#A31515;">&quot;&quot;</span>)
        


<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Union types</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> UnionTypes = 

    <span style="color:Gray;">///</span><span style="color:Green;"> Represents the suit of a playing card</span>
    <span style="color:Blue;">type</span> Suit = 
        | Hearts 
        | Clubs 
        | Diamonds 
        | Spades

    <span style="color:Gray;">///</span><span style="color:Green;"> Represents the rank of a playing card</span>
    <span style="color:Blue;">type</span> Rank = 
        <span style="color:Gray;">///</span><span style="color:Green;"> Represents the rank of cards 2 .. 10</span>
        | Value <span style="color:Blue;">of</span> int
        | Ace
        | King
        | Queen
        | Jack
        <span style="color:Blue;">static</span> <span style="color:Blue;">member</span> GetAllRanks() = 
            [ <span style="color:Blue;">yield</span> Ace
              <span style="color:Blue;">for</span> i <span style="color:Blue;">in</span> 2 .. 10 <span style="color:Blue;">do</span> <span style="color:Blue;">yield</span> Value i
              <span style="color:Blue;">yield</span> Jack
              <span style="color:Blue;">yield</span> Queen
              <span style="color:Blue;">yield</span> King ]
                                   
    <span style="color:Blue;">type</span> Card =  { Suit: Suit; Rank: Rank }
              
    <span style="color:Gray;">///</span><span style="color:Green;"> Returns a list representing all the cards in the deck</span>
    <span style="color:Blue;">let</span> fullDeck = 
        [ <span style="color:Blue;">for</span> suit <span style="color:Blue;">in</span> [ Hearts; Diamonds; Clubs; Spades] <span style="color:Blue;">do</span>
              <span style="color:Blue;">for</span> rank <span style="color:Blue;">in</span> Rank.GetAllRanks() <span style="color:Blue;">do</span> 
                  <span style="color:Blue;">yield</span> { Suit=suit; Rank=rank } ]

    <span style="color:Gray;">///</span><span style="color:Green;"> Converts a 'Card' object to a string</span>
    <span style="color:Blue;">let</span> showCard c = 
        <span style="color:Blue;">let</span> rankString = 
            <span style="color:Blue;">match</span> c.Rank <span style="color:Blue;">with</span> 
            | Ace <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;Ace&quot;</span>
            | King <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;King&quot;</span>
            | Queen <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;Queen&quot;</span>
            | Jack <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;Jack&quot;</span>
            | Value n <span style="color:Blue;">-&gt;</span> string n
        <span style="color:Blue;">let</span> suitString = 
            <span style="color:Blue;">match</span> c.Suit <span style="color:Blue;">with</span> 
            | Clubs <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;clubs&quot;</span>
            | Diamonds <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;diamonds&quot;</span>
            | Spades <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;spades&quot;</span>
            | Hearts <span style="color:Blue;">-&gt;</span> <span style="color:#A31515;">&quot;hearts&quot;</span>
        rankString  + <span style="color:#A31515;">&quot; of &quot;</span> + suitString

    <span style="color:Blue;">let</span> printAllCards() = 
        <span style="color:Blue;">for</span> card <span style="color:Blue;">in</span> fullDeck <span style="color:Blue;">do</span> 
            printfn <span style="color:#A31515;">&quot;%s&quot;</span> (showCard card)



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Option types</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> OptionTypes = 
    <span style="color:Gray;">///</span><span style="color:Green;"> Option values are any kind of value tagged with either 'Some' or 'None'.</span>
    <span style="color:Gray;">///</span><span style="color:Green;"> They are used extensively in F# code to represent the cases where many other</span>
    <span style="color:Gray;">///</span><span style="color:Green;"> languages would use null references.</span>

    <span style="color:Blue;">type</span> Customer = { zipCode : decimal option }

    <span style="color:Gray;">///</span><span style="color:Green;"> Abstract class that computes the shipping zone for the customer's zip code, </span>
    <span style="color:Gray;">///</span><span style="color:Green;"> given implementations for the 'getState' and 'getShippingZone' abstract methods.</span>
    [&lt;AbstractClass&gt;]
    <span style="color:Blue;">type</span> ShippingCalculator =
        <span style="color:Blue;">abstract</span> getState : decimal <span style="color:Blue;">-&gt;</span> string option
        <span style="color:Blue;">abstract</span> getShippingZone : string <span style="color:Blue;">-&gt;</span> int

        <span style="color:Gray;">///</span><span style="color:Green;"> Return the shipping zone corresponding to the customer's ZIP code</span>
        <span style="color:Gray;">///</span><span style="color:Green;"> Customer may not yet have a ZIP code or the ZIP code may be invalid</span>
        <span style="color:Blue;">member</span> this.customerShippingZone(customer : Customer) =
            customer.zipCode |&gt; Option.bind this.getState |&gt; Option.map this.getShippingZone



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Pattern matching</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> PatternMatching = 

    <span style="color:Gray;">///</span><span style="color:Green;"> A record for a person's first and last name</span>
    <span style="color:Blue;">type</span> Person = {     
        First : string
        Last  : string
    }

    <span style="color:Gray;">///</span><span style="color:Green;"> define a discriminated union of 3 different kinds of employees</span>
    <span style="color:Blue;">type</span> Employee = 
        | Engineer  <span style="color:Blue;">of</span> Person
        | Manager   <span style="color:Blue;">of</span> Person * list&lt;Employee&gt;            <span style="color:Green;">// manager has list of reports</span>
        | Executive <span style="color:Blue;">of</span> Person * list&lt;Employee&gt; * Employee <span style="color:Green;">// executive also has an assistant</span>

    <span style="color:Gray;">///</span><span style="color:Green;"> count everyone underneath the employee in the management hierarchy, including the employee</span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> countReports(emp : Employee) = 
        1 + <span style="color:Blue;">match</span> emp <span style="color:Blue;">with</span>
            | Engineer(id) <span style="color:Blue;">-&gt;</span> 
                0
            | Manager(id, reports) <span style="color:Blue;">-&gt;</span> 
                reports |&gt; List.sumBy countReports 
            | Executive(id, reports, assistant) <span style="color:Blue;">-&gt;</span>
                (reports |&gt; List.sumBy countReports) + countReports assistant


    <span style="color:Gray;">///</span><span style="color:Green;"> find all managers/executives named &quot;Dave&quot; who do not have any reports</span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> findDaveWithOpenPosition(emps : Employee list) =
        emps 
        |&gt; List.filter(<span style="color:Blue;">function</span> 
                       | Manager({First = <span style="color:#A31515;">&quot;Dave&quot;</span>}, []) <span style="color:Blue;">-&gt;</span> <span style="color:Blue;">true</span>       <span style="color:Green;">// [] matches the empty list</span>
                       | Executive({First = <span style="color:#A31515;">&quot;Dave&quot;</span>}, [], _) <span style="color:Blue;">-&gt;</span> <span style="color:Blue;">true</span>
                       | _ <span style="color:Blue;">-&gt;</span> <span style="color:Blue;">false</span>)                                 <span style="color:Green;">// '_' is a wildcard pattern that matches anything</span>
                                                                     <span style="color:Green;">// this handles the &quot;or else&quot; case</span>



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Units of measure</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> UnitsOfMeasure = 

    <span style="color:Green;">// Code can be annotated with units of measure when using F# arithmetic over numeric types</span>

    <span style="color:Blue;">open</span> Microsoft.FSharp.Data.UnitSystems.SI.UnitNames

    [&lt;Measure&gt;]
    <span style="color:Blue;">type</span> mile =
        <span style="color:Gray;">///</span><span style="color:Green;"> Conversion factor mile to meter: meter is defined in SI.UnitNames</span>
        <span style="color:Blue;">static</span> <span style="color:Blue;">member</span> asMeter = 1600.&lt;meter/mile&gt;

    <span style="color:Blue;">let</span> d  = 50.&lt;mile&gt;          <span style="color:Green;">// Distance expressed using imperial units</span>
    <span style="color:Blue;">let</span> d' = d * mile.asMeter   <span style="color:Green;">// Same distance expressed using metric system</span>

    printfn <span style="color:#A31515;">&quot;%A = %A&quot;</span> d d'
    <span style="color:Green;">// let error = d + d'       // Compile error: units of measure do not match</span>



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Parallel array programming</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> ParallelArrayProgramming = 
              
    <span style="color:Blue;">let</span> oneBigArray = [| 0 .. 100000 |]
    
    <span style="color:Green;">// do some CPU intensive computation </span>
    <span style="color:Blue;">let</span> <span style="color:Blue;">rec</span> computeSomeFunction x = 
        <span style="color:Blue;">if</span> x &lt;= 2 <span style="color:Blue;">then</span> 1 
        <span style="color:Blue;">else</span> computeSomeFunction (x - 1) + computeSomeFunction (x - 2)
       
    <span style="color:Green;">// Do a parallel map over a large input array</span>
    <span style="color:Blue;">let</span> computeResults() = oneBigArray |&gt; Array.Parallel.map (<span style="color:Blue;">fun</span> x <span style="color:Blue;">-&gt;</span> computeSomeFunction (x % 20))

    printfn <span style="color:#A31515;">&quot;Parallel computation results: %A&quot;</span> (computeResults())



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Using events</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> Events = 

    <span style="color:Blue;">open</span> System

    <span style="color:Green;">// create instance of Event object that consists of subscription point (event.Publish) and event trigger (event.Trigger)</span>
    <span style="color:Blue;">let</span> simpleEvent = <span style="color:Blue;">new</span> Event&lt;int&gt;() 

    <span style="color:Green;">// add handler</span>
    simpleEvent.Publish.Add(
        <span style="color:Blue;">fun</span> x <span style="color:Blue;">-&gt;</span> printfn <span style="color:#A31515;">&quot;this is handler was added with Publish.Add: %d&quot;</span> x)

    <span style="color:Green;">// trigger event</span>
    simpleEvent.Trigger(5)

    <span style="color:Green;">// create instance of Event that follows standard .NET convention: (sender, EventArgs)</span>
    <span style="color:Blue;">let</span> eventForDelegateType = <span style="color:Blue;">new</span> Event&lt;EventHandler, EventArgs&gt;()    

    <span style="color:Green;">// add handler</span>
    eventForDelegateType.Publish.AddHandler(
        EventHandler(<span style="color:Blue;">fun</span> _ _ <span style="color:Blue;">-&gt;</span> printfn <span style="color:#A31515;">&quot;this is handler was added with Publish.AddHandler&quot;</span>))

    <span style="color:Green;">// trigger event (note that sender argument should be set)</span>
    eventForDelegateType.Trigger(<span style="color:Blue;">null</span>, EventArgs.Empty)



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         Database access using type providers</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> DatabaseAccess = 
              
    <span style="color:Green;">// The easiest way to access a SQL database from F# is to use F# type providers. </span>
    <span style="color:Green;">// Add references to System.Data, System.Data.Linq, and FSharp.Data.TypeProviders.dll.</span>
    <span style="color:Green;">// You can use Server Explorer to build your ConnectionString. </span>

    <span style="color:Green;">(*
    #r &quot;System.Data&quot;
    #r &quot;System.Data.Linq&quot;
    #r &quot;FSharp.Data.TypeProviders&quot;

    open Microsoft.FSharp.Data.TypeProviders
    
    type SqlConnection = SqlDataConnection&lt;ConnectionString = @&quot;Data Source=.\sqlexpress;Initial Catalog=tempdb;Integrated Security=True&quot;&gt;
    let db = SqlConnection.GetDataContext()

    let table = 
        query { for r in db.Table do
                select r }
    *)</span>


    <span style="color:Green;">// You can also use SqlEntityConnection instead of SqlDataConnection, which accesses the database using Entity Framework.</span>

    ()



<span style="color:Green;">// ---------------------------------------------------------------</span>
<span style="color:Green;">//         OData access using type providers</span>
<span style="color:Green;">// ---------------------------------------------------------------</span>

<span style="color:Blue;">module</span> OData = 

    <span style="color:Green;">(*
    open System.Data.Services.Client
    open Microsoft.FSharp.Data.TypeProviders

    // Consume demographics population and income OData service from Azure Marketplace. 
    // For more information, see http://go.microsoft.com/fwlink/?LinkId=239712
    type Demographics = Microsoft.FSharp.Data.TypeProviders.ODataService&lt;ServiceUri = &quot;https://api.datamarket.azure.com/Esri/KeyUSDemographicsTrial/&quot;&gt;
    let ctx = Demographics.GetDataContext()

    // Sign up for a Azure Marketplace account at https://datamarket.azure.com/account/info
    ctx.Credentials &lt;- System.Net.NetworkCredential (&quot;&lt;your liveID&gt;&quot;, &quot;&lt;your Azure Marketplace Key&gt;&quot;)

    let cities = query {
        for c in ctx.demog1 do
        where (c.StateName = &quot;Washington&quot;)
        } 

    for c in cities do
        printfn &quot;%A - %A&quot; c.GeographyId c.PerCapitaIncome2010.Value
    *)</span>

    ()



<span style="color:Blue;">#if</span> COMPILED
<span style="color:Blue;">module</span> BoilerPlateForForm = 
    [&lt;System.STAThread&gt;]
    <span style="color:Blue;">do</span> ()
    <span style="color:Blue;">do</span> System.Windows.Forms.Application.Run()
<span style="color:Blue;">#endif</span>

</pre></div>